package com.cqh.recruit.controller.school;



import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cqh.recruit.pojo.dto.StudentQueryDto;
import com.cqh.recruit.pojo.entity.Student;
import com.cqh.recruit.pojo.vo.StudentListVo;
import com.cqh.recruit.result.Result;
import com.cqh.recruit.service.IStudentService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;


/**
 *
 *  前端控制器
 *
 *
 * @author dh
 * @since 2025-10-18
 * @version v1.0
 */
@RestController("schoolStudentController")
@RequestMapping("/api/school/student")
public class StudentController {

    @Autowired
    private IStudentService studentService;

    @GetMapping("/find")
    @Operation(summary = "根据id查询学生")
    public Result find(Long id) {
        if (id == null) {
            return Result.buildFail(2001, "id不能为空");
        }
        Student student = studentService.getById(id);
        return Result.buildSuccess(student);
    }

    @PostMapping("/add")
    @Operation(summary = "添加学生")
    public Result add(@RequestBody Student  student) {

        if (student.getPassword() == null || student.getPassword().trim().isEmpty()) {
            student.setPassword("123456"); // 默认密码
        }
        student.setCreateTime(LocalDateTime.now());
        studentService.save(student);
        return Result.buildSuccess();
    }
    @PostMapping("/update")
    @Operation(summary = "更新学生数据")
    public Result update(@RequestBody Student student) {
        if (student.getPassword() == null || student.getPassword().trim().isEmpty()) {
            student.setPassword("123456"); // 默认密码
        }
        student.setUpdateTime(LocalDateTime.now());
        studentService.updateById(student);
        return Result.buildSuccess();
    }
    @GetMapping("/delete")
    @Operation(summary = "删除学生")
    public Result delete(Long id) {
        if (id == null) {
            return Result.buildFail(2001, "id不能为空");
        }
        studentService.removeById(id);
        return Result.buildSuccess();
    }
    @PostMapping("/list")
    @Operation(summary = "查询学生列表")
    public Result list(@RequestBody StudentQueryDto studentQueryDto) {
        Page<StudentListVo> resultPage = studentService.findStudentByQueryDto(studentQueryDto);
        return Result.buildSuccess(resultPage.getRecords(), resultPage.getTotal());
    }


}

